package cn.zymall.user.service.impl;

import cn.zymall.common.model.Collect;
import cn.zymall.common.model.Footprint;
import cn.zymall.common.model.Spu;
import cn.zymall.user.mapper.FootprintMapper;
import cn.zymall.user.service.FootprintService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author : th
 * @ClassName: FootprintImpl
 * @Description : 足迹实现类
 * @date 2020年08月19日 11:17
 **/
@Service
public class FootprintImpl extends ServiceImpl<FootprintMapper, Footprint> implements FootprintService {


    @Autowired
    private FootprintMapper mapper;

    /**
     * @author：th
     * @Description：查询所有足迹
     * @Param：用户ID
     * @return：List<Footprint>
     **/
    @Override
    public PageInfo<Spu> footAll(@Param("id") int id, @Param("page")int page, @Param("rows")int rows) {
        PageHelper.startPage(page,rows);
        List<Spu>  list=mapper.footAll(id);
        PageInfo<Spu> pageaa = new PageInfo<>(list);
        return pageaa;
    }


    /**
     * @author：th
     * @Description：模糊查询
     * @Param：商品名称
     * @return：List<Spu>
     **/
    @Override
    public PageInfo<Spu> footQuzzy(@Param("id") int id, String value,@Param("page")int page, @Param("rows")int rows) {

        PageHelper.startPage(page,rows);
        List<Spu>  list=mapper.footQuzzy(id,value);
        PageInfo<Spu> pageaa = new PageInfo<>(list);
        return pageaa;
    }


    /**
     * @author：th
     * @Description：id查询
     * @Param：用户ID，商品Id
     * @return：Footprint
     **/
    @Override
    public Footprint footId(int id, int fid){
        return mapper.footId(id,fid);
    }

	/**
	 * 我修改
	 * @param footprint
	 * @return
	 */
	 @Override
	 public boolean myUpdate(Footprint footprint){
		 Spu spu = mapper.selectByStuId(footprint.getSpuId());
		 if(spu==null){
			 return false;
		 }
		 if (footId(footprint.getUserId(),footprint.getSpuId()) != null){
			 return false;
		 }
		return mapper.updateById(footprint)>0?true:false;
	 }

	/**
	 * 我新增
	 * @param footprint
	 * @return
	 */
	@Override
	public boolean mySave(Footprint footprint){
		System.out.println(footprint);
		Spu spu = mapper.selectByStuId(footprint.getSpuId());
		System.out.println(spu);
		if(spu==null){
			return false;
		}
		if (footId(footprint.getUserId(),footprint.getSpuId()) != null){
			footprint.setFootprintTime(new Date());
			return myUpdate(footprint);
		}
		return this.mapper.insert(footprint)>0;
	}

	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@Override
	public boolean mydelete(int id) {
		Footprint footprint=new Footprint();
		footprint.setFootprintId(id);
		footprint.setFootprintStatus((byte)0);
		return mapper.updateById(footprint)>0;
	}


	/**
     * @author：th
     * @Description：修改
     * @Param：footprint
     * @return：boolean
     **/
    @Override
    public boolean footSave(Footprint footprint){
	    Spu spu = mapper.selectByStuId(footprint.getSpuId());
	    if(spu==null){
		    return false;
	    }
        int id = footprint.getUserId();
        int fid = footprint.getSpuId();
        if (footId(id,fid) != null){
        	footprint.setFootprintTime(new Date());
            return myUpdate(footprint);
        }else{
            footprint.setFootprintTime(new Date());
	        return myUpdate(footprint);
        }
    }


    /**
     * @author：th
     * @Description：修改足迹表
     * @Param：用户ID，商品ID
     * @return：Boolean
     **/
    @Override
    public boolean footUpdate(int id,int fid){
        return mapper.footUpdate(id,fid);
    }



    /**
     * @author：th
     * @Description：足迹表删除
     * @Param：用户ID，商品编号list
     * @return：Boolean
     **/
    @Override
    public boolean footRemove(int id,List list){
        return mapper.footRemove(id,list);
    }


	/**
	 * @Author 欧泽昌
	 * @Description //获取date类型时间用户的足迹商品
	 * @Date 2020/8/27 10:41
	 * @Param [id]
	 * @return java.util.List<cn.zymall.common.model.Footprint>
	 **/
	@Override
	public List<Footprint> footAllDate(int id){
		return this.mapper.footAllDate(id);
	};



















}
